package nio.book;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件复制
 */
public class NioDemo1 {

    public static void main(String[] args) throws Exception {
        fileCopy("D:\\Downloads\\Prison.Break.S02E13.1080p.BluRay.x264-FLHD.mkv","D:\\Downloads\\复制文件.png");
    }

    public static void fileCopy(String srcPath,String targetPath) throws Exception{
        File ofile = new File(targetPath);
        File ifile = new File(srcPath);


        if (!ofile.exists()){
            ofile.createNewFile();
        }

        FileChannel inChannel = new FileInputStream(ifile).getChannel();
        FileChannel outChannel = new FileOutputStream(ofile).getChannel();


        ByteBuffer buf = ByteBuffer.allocate(1024*1024*200);

        int length;

        while ((length=inChannel.read(buf))!=-1){
            buf.flip();
            System.out.println("读取字节长度："+length);
            int outlength;
            while ((outlength=outChannel.write(buf))!=0){
                System.out.println("写入字节长度:"+outlength);
            }

            buf.clear();
        }

        System.out.println("复制完成");

    }
}
